package com.wyx.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyx.Common.MyException;
import com.wyx.Common.R;
import com.wyx.Dao.CategoryDao;
import com.wyx.Dao.EmployeeDao;
import com.wyx.Pojo.Category;
import com.wyx.Pojo.Dish;
import com.wyx.Pojo.Employee;
import com.wyx.Pojo.Setmeal;
import com.wyx.Service.CategoryService;
import com.wyx.Service.DishService;
import com.wyx.Service.EmployeeService;
import com.wyx.Service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//P8 继承mabatisplus
@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public int deletebyids(Long ids) {
        LambdaQueryWrapper<Dish> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(wrapper);
        if (count1>0){
            //抛一个自定义的异常
            throw new MyException("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(wrapper2);
        if (count2>0){
            throw new MyException("该分类下关联了套餐，不能删除");
        }
        return categoryDao.deleteById(ids);
    }
}
